<? 
	if(isset($_POST['btnsubmit']))
	{
		require("required/dbconnect.php");
		require("required/function.php");
		require("../required/share_function.php");
		require("../required/email.php");
		$status=$_POST['status'];
		$id=$_POST['id'];
			
		
		if($status==2)
		{
			$update=mysql_query("update os_bill_details set status=$status,last_update_by_machant=now(),verified_date=now() where id=$id");
			
			$get_items=mysql_query("select a.id,a.item_id,a.quantity,b.pid from os_bill_items a inner join os_product_inventory b on a.item_id = b.id where a.bid=$id");
			
			while($i=mysql_fetch_array($get_items,MYSQL_ASSOC))
			{
				$item_id=$i['item_id'];
				$total_deduct=$i['quantity'];
				$pid=$i['pid'];
				updateInventory($item_id,$total_deduct,$id,$pid);
			}
			email_invoice($id);
			$action=2;
		}
		elseif($status==4)
		{
			$update=mysql_query("update os_bill_details set status=$status,last_update_by_machant=now() where id=$id");
			email_receipt($id);
			$action=3;
		}
		elseif($status==5)
		{
			$get_bill=mysql_query("select * from os_bill_details where id=$id");
			$b=mysql_fetch_array($get_bill,MYSQL_ASSOC);
			
			if($b['courier_name']=="" || $b['ref_no']=="" || $b['eta']=="")
			{
				$action=10;
			}else{
				$update=mysql_query("update os_bill_details set status=$status,last_update_by_machant=now() where id=$id");
				email_shipmentinfo($id);
				$action=5;
			}
		}
		elseif($status==8)
		{
			header("Location:order_delete.php?id=$id");
		}
		elseif($status==9)
		{
						
			$update_status=mysql_query("update os_bill_details set sale_close=1,status=9 where id=$id")or die(mysql_error());
			
			$action=9;
		}
		else
		{
			$update=mysql_query("update os_bill_details set status=$status,last_update_by_machant=now() where id=$id");
			$action=1;
		}
			
		header("Location:order_viewfull.php?id=$id&action=$action");
	
	}
	
	if(isset($_POST['btnsendemail']))
	{
		require("required/dbconnect.php");
		require("required/function.php");
		
		require("../required/email.php");
		$status=$_POST['status'];
		$id=$_POST['id'];
			
		
		if($status==2)
		{
			
			email_invoice($id);
			$action=2;
		}
		elseif($status==4)
		{
			email_receipt($id);
			$action=3;
		}
		elseif($status==5)
		{
			$get_bill=mysql_query("select * from os_bill_details where id=$id");
			$b=mysql_fetch_array($get_bill,MYSQL_ASSOC);
			
			if($b['courier_name']=="" || $b['ref_no']=="" || $b['eta']=="")
			{
				$action=10;
			}else{
				email_shipmentinfo($id);
				$action=5;
			}
		}
		
			
		header("Location:order_viewfull.php?id=$id&action=$action");
	
	}
	
	
	
	if(isset($_POST['btnupdate']))
	{
		require("required/dbconnect.php");
		
		$id=$_POST['id'];
		$transport_fees=$_POST['transport_fees'];
		$discount=$_POST['discount'];
		$update=mysql_query("update os_bill_details set transport_fees='$transport_fees',discount='$discount',last_update_by_machant=now() where id=$id");
		
		header("Location:order_viewfull.php?id=$id");
	
	}
	if(isset($_POST['btnupdate_courier']))
	{
		require("required/dbconnect.php");
		
		$id=$_POST['id'];
		$courier_name=$_POST['courier_name'];
		$ref_no=$_POST['ref_no'];
		$eta=$_POST['eta'];
		$update=mysql_query("update os_bill_details set courier_name='$courier_name',ref_no='$ref_no',eta='$eta' where id=$id");
		
		header("Location:order_viewfull.php?id=$id&action=4");
	
	}
	
	if(isset($_POST['btnupdate_info']))
	{
		require("required/dbconnect.php");
		
		$id=$_POST['id'];
		$name=$_POST['name'];
		$hpnumber=$_POST['contact_number'];
		
		$address1=$_POST['address1'];
		$address2=$_POST['address2'];
		$postcode=$_POST['postcode'];
		$country=$_POST['country'];
		$city=$_POST['city'];
		$state=$_POST['state'];
		$email=$_POST['email'];
		$message=mysql_escape_string($_POST['message']);
		//echo "update os_bill_details set receiver_name='$name',address1='$address1',address2='$address2',postcode='$postcode',city='$city',country='$country',state='$state,contact_number='$hpnumber',email='$email',message='$message' where id=$id";
		$update=mysql_query("update os_bill_details set receiver_name='$name',address1='$address1',address2='$address2',postcode='$postcode',city='$city',country='$country',state='$state',contact_number='$hpnumber',email='$email',message='$message' where id=$id")or die(mysql_error());
		
		header("Location:order_viewfull.php?id=$id&action=6");
	
	}

	require("required/dbconnect.php"); 
	require("required/function.php");
	require("required/header.php");
	
	
	$bid=$_GET['id'];
	
	$get_bill=mysql_query("select * from os_bill_details where id=$bid");
	$b=mysql_fetch_array($get_bill,MYSQL_ASSOC);
	
	$get_item=mysql_query("select * from os_bill_items where bid=$bid")or die("line 11".mysql_error());
	
	if(empty($_GET['action']))
	{
		$action_msg="";
		
	}
	else
	{
		$action=$_GET['action'];
		
		if($action==1)
		{
			$action_msg="<div class='dw_success'> Record Updated</div>";
		}
		elseif($action==2)
		{
			$action_msg="<div class='dw_success'> An Invoice has been sent to buyer's email</div>";
		}
		elseif($action==3)
		{
			$action_msg="<div class='dw_success'> A receipt to this payment has been sent to the buyer��s email</div>";
		}
		elseif($action==4)
		{
			$action_msg="<div class='dw_success'> Courier Info updated</div>";
		}
		elseif($action==5)
		{
			$action_msg="<div class='dw_success'> Courier Info has been sent to buyer's email</div>";
		}
		elseif($action==6)
		{
			$action_msg="<div class='dw_success'> Receiver Details Updated</div>";
		}
		elseif($action==9)
		{
			$action_msg="<div class='dw_success'> Sale Closed</div>";
		}
		elseif($action==10)
		{
			$action_msg="<div class='dw_failed'> Please complete courier information</div>";
		}
		else
		{
			$action_msg="";
		}
	}
	
	
	
	
	
	$orange=$b['survey_1']*10;
	$gray=50-($b['survey_1']*10);
	
?>

<script>
		function formvalidate(frm1)
	{
		
		if(document.frm1.email.value=="")
		{
			alert("Please enter your email");
			document.frm1.email.focus();
			return false;
		}
		if (document.frm1.email.value!="")
 		{
			var x = document.frm1.email.value;
			var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if (filter.test(x)==false)
			{ 
				alert('Please enter a valid email address');
				document.frm1.email.focus();
				return false;
			}
		
  		}
		if(document.frm1.password.value=="")
		{
			alert("Please enter password");
			document.frm1.password.focus();
			return false;
		}
		if(document.frm1.password.value.length<6)
		{
			alert("password length must more than 6 characters");
			document.frm1.password.focus();
			return false;
		}
		if(document.frm1.password.value.indexOf(" ")>-1)
		{
			alert("spaces not allowed!");
			document.frm1.password.focus();
			return false;
		}
		if(document.frm1.cpassword.value=="")
		{
			alert("Please reenter your password");
			document.frm1.cpassword.focus();
			return false;
		}
		if(document.frm1.password.value!=document.frm1.cpassword.value)
		{
			alert("Password doesn't match, please check");
			document.frm1.cpassword.focus();
			return false;
		}
		if(document.frm1.name.value=="")
		{
			alert("Please enter your name");
			document.frm1.name.focus();
			return false;
		}
		if(document.frm1.address1.value=="")
		{
			alert("Please enter your address");
			document.frm1.address1.focus();
			return false;
		}
		if(document.frm1.email.value=="")
		{
			alert("Please enter your email");
			document.frm1.email.focus();
			return false;
		}
		if(document.frm1.email.value!="")
		{
		 	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		   	var address = document.frm1.email.value;
		   	if(reg.test(address) == false) {
				alert("Invalid email");
				document.frm1.email.focus();
				return false;
			 }
		}
		if(document.frm1.verification_code.value=="")
		{
			alert("Please enter verification_code");
			document.frm1.verification_code.focus();
			return false;
		}
		
		
		return true;
	}
</script>
<table width="100%">
	
		<Td valign="top">
		<?= $action_msg ?>
			<table width="100%">
			<tr>
				<td width="100%">
				<table width="100%">
				
				<td width="20%" colspan="4">Order Status : <font color="#0066CC"><?= billstatus_eng($b['status']) ?></font></td>
				<tr>
				<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" name="frm">
				<td width="50%" valign="middle" align="left">
				<?
					if($b['status']==1 || $b['status']==3 || $b['status']==4  || $b['status']==10 ){
				?>
				
				
				Change status to 
					<select name="status" style="color:#0066CC;font-size:10px ">
						<?
							if($b['status']==1)
							{
						?>
						<option value="2">Order verified</option>
						<? } ?>
						<?
							if($b['status']==3 || $b['status']==2)
							{
						?>
						<option value="4">Merchant already received payment</option>
						<? } ?>
						
						<?
							if($b['status']==4 || $b['status']==10)
							{
						?>
						<option value="5">Merchant shipped the goods</option>
						<? } ?>
					</select>
					<input type="hidden" value="<?= $b['id'] ?>" name="id">
					<input type="submit" value="Go" class="button" name="btnsubmit">
				
				
				<? } ?>
				</td>	
				</form>	
				<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" name="frm">
				<td width="50%" valign="middle" align="right">
				<?
					if($b['status']>1){
				?>
				
				Resend
					<select name="status" style="color:#0066CC;font-size:10px ">
						<?
							if($b['status']==2)
							{
						?>
						<option value="2">Invoice</option>
						<? }
							elseif($b['status']==3)
							{
						?>
						<option value="2">Invoice</option>
						<option value="4">Receipt</option>
						<? }elseif($b['status']==4 || $b['status']==5 || $b['status']==10)
							{
						?>
						<option value="2">Invoice</option>
						<option value="4">Receipt</option>
						<option value="5">Courier Info</option>
						<? } ?>
					</select>
					<input type="hidden" value="<?= $b['id'] ?>" name="id">
					<input type="submit" value="submit" class="button" name="btnsendemail">
				
				<? } ?>
				</td>	
				</form>	
				</tr>
				
				<tr>
					
				</tr>
				
				<tr>
					<td colspan="3">
						<table  align="center" cellpadding="5" cellspacing="0">
					<? 
							if($b['status']<3)
							{
					?>
					<td align="center" valign="top">
						
						<input type="button" value="Delete Order" class="button" onClick="window.open('order_delete.php?id=<?= $bid ?>','_self')">
						
					</td>
					<? 
					}else{
					
						if($b['sale_close']==0){
				?>
				
					<Td align="center" valign="top" >
					<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" name="frm">
					<table width="100%" cellpadding="0" cellspacing="0">
						<td >
							<input type="submit" value="Close This Sales" name="btnsubmit" class="button">
							<input type="hidden" name="status" value="9">
							<input type="hidden" name="id" value="<?= $b['id'] ?>">
														
						</td>
						
					</table>
					</form>
					</Td>
				
				
				<? }} ?>
					<td valign="top">
					
					<?
					 
							if($b['status']>=2)
							{
					?>
						<input type="button" value="Make Payment Record" class="button" onClick="window.open('order_paymentmade.php?id=<?= $bid ?>','_self')">
					<? } ?>	
					</td>
					</table>
					</td>
				</tr>
				</table>
				<br>
				<?
					if($b['goods_received_date']!='0000-00-00')
					{
				?>
				<table width="100%" cellpadding="0" cellspacing="0">
				<td width="33%">Received Date</td><td width="60%">: <font color="#0066CC"><?= $b['goods_received_date'] ?></font></td>
				<td width="7%" rowspan="3" align="right">
				</td>
				
				<tr>
				<td>Rate for this purchase</td><td>: <img src="images/1x1_orange.gif" width="<?= $orange ?>" height="5" align="middle"><img src="images/1x1_gray.gif" width="<?= $gray ?>" height="5" align="middle"> <?= scoreBar($b['survey_1']) ?></td>
				</tr>
				<tr>
				<td>Comment for this purchase</td><td>: <?= $b['review'] ?></td>
				</tr>
				</table>
				<? } ?>
				
				<table width="100%" bgcolor="#CCCCCC" cellpadding="1" cellspacing="1">
				<tr bgcolor="#F3F3F3" align="center">
					<td width="42%">Product</td><td width="13%">Price(RM)</td><td width="15%">Product code</td><td width="11%">Quantity</td><td width="15%">Total(RM)</td>
				</tr>
			<?
					$total_item=0;
					$total_piece=0;
					$total_allprice=0;
					while($c=mysql_fetch_array($get_item))
					{
						$total_price=0;
						$get_product=mysql_query("select a.product_name_eng,a.product_code,b.description,b.unit_price from os_products a inner join os_product_inventory b on a.id=b.pid where b.id=".$c['item_id']);
						$a=mysql_fetch_array($get_product,MYSQL_ASSOC);
						$total_price=$a['unit_price']*$c['quantity'];
						$total_allprice=$total_allprice+$total_price;
						
																
			?>
				<tr bgcolor="#FFFFFF" align="center">
					<td align="left"><a href="product_viewfull.php?id=<?= $c['pid'] ?>"><?= $a['product_name_eng'] ?></a>
					<br>Product Option : <strong><?= $a['description'] ?></strong></td><td><?= $a['unit_price'] ?></td><td><?= $a['product_code'] ?></td><td><?= $c['quantity'] ?></td>
					<td align="right"><?= number_format($total_price,2) ?></td>
				</tr>
			<? 
				
					} 
			?>
            <tr>
            	<Td colspan="5">
                	<form method="post" action="order_modify_item.php">
                		<input type="submit" value="Modify" class="button" />
                    	<input type="hidden" value="<?= $bid ?>" name="bid" />
                    </form>
                </Td>
            </tr>
			</table>
			<br><br>
			</td>
			</tr>
			
			<tr>
				<td>
				<fieldset><legend><strong>Order Details</strong></legend>
				<table width="100%">
				<tr>
				<td width="13%">Order Number</td><td width="1%">:</td><td width="29%"><?= $b['invoice_number'] ?>	</td>
				<td width="21%">Total Product Amount(RM)</td><td width="1%">:</td><td width="35%"><?= number_format($b['total_amount'],2) ?></td>
				</tr>
				<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>" name="frm">
				<tr>
				<td width="13%">Order Date</td><td width="1%">:</td><td width="29%"><?= $b['create_date'] ?></td>
				<td width="21%">Discount</td><td width="1%">:</td><td width="35%"><input type="text" value="<?= $b['discount'] ?>" name="discount" size="2" class="text" maxlength="2">%</td>
				
				</tr>
				<tr>
				<td width="13%">Service Preferred</td><td width="1%">:</td><td width="29%"><?= servicepreferred($b['service_preferred']) ?></td>
				<td width="21%">Transport Fees</td><td width="1%">:</td><td width="35%"><input type="text" value="<?= $b['transport_fees'] ?>" name="transport_fees" size="3" class="text" maxlength="5"></td>
				</tr>
				<?
					$total_amount=$b['total_amount']; 
					$discount=($b['discount']/100)*$total_amount;
					$final_amount=($total_amount-$discount)+$b['transport_fees'];
				?>
				<tr>
				<td width="13%"></td><td width="1%"></td><td width="29%"></td>
				<td width="21%">Total Overall Amount</td><td width="1%">:</td><td width="35%"><?= number_format($final_amount,2) ?></td>
				</tr>
				<tr>
				<input type="hidden" value="<?= $b['id'] ?>" name="id">
				<td width="13%" colspan="8"><input type="submit" value="Update" class="button" name="btnupdate"></td>
				</tr>
				</form>
				</table>
				</fieldset>
				<br><br>
				</td>
			</tr>
			
			
			<tr>
				<td>
				<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>" name="frm1"  onSubmit="return formvalidate(this)">
				<fieldset><legend><strong>Courier Information</strong></legend>
				<table width="100%">
				<tr>
				<td width="20%">Courier name</td><td width="1%">:</td><td width="79%"><input type="text" name="courier_name" maxlength="100" class="text" value="<?= $b['courier_name'] ?>"></td>
				</tr>
				<tr>
				<td width="20%">Reference number</td><td width="1%">:</td><td width="79%"><input type="text" name="ref_no" maxlength="20" class="text" value="<?= $b['ref_no'] ?>"></td>
				</tr>
				<tr>
				<td width="20%">Estimate Time of Arrival</td><td width="1%">:</td><td width="79%"><input type="text" name="eta" maxlength="20" class="text" value="<?= $b['eta'] ?>"> e.g :<?= date("Y-m-d") ?></td>
				</tr>
				<tr>
				<input type="hidden" value="<?= $b['id'] ?>" name="id">
				
				<td width="20%" colspan="3"><input type="submit" value="Update" class="button" name="btnupdate_courier"></td>
				</tr>
				
				</table>
				</fieldset>
				</form>
				<br>
				</td>
			</tr>
			
			<tr>
				<td>
				<fieldset><legend><strong>Receiver Details</strong></legend>
				<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
				<table width="100%">
				<tr>
				<td width="13%">Name</td><td width="1%">:</td><td width="29%">
				<input type="text" name="name" value="<?= $b['receiver_name'] ?>" size="30" maxlength="100">
				</td>
				<td width="16%">Email</td><td width="1%">:</td><td width="40%">
				<input type="text" name="email" value="<?= $b['email'] ?>" size="30" maxlength="100">
				</td>
				</tr>
				<tr>
				<td width="13%">Address</td><td width="1%">:</td><td width="29%">
				<input type="text" name="address1" value="<?= $b['address1'] ?>" size="30" maxlength="200">
				</td>
				<td width="16%">Contact Number</td><td width="1%">:</td><td width="40%">
				<input type="text" name="contact_number" value="<?= $b['contact_number'] ?>" size="30" maxlength="100">
				</td>
				<tr>
				<td colspan="2"></td><td width="29%">
				<input type="text" name="address2" value="<?= $b['address2'] ?>" size="30" maxlength="200">
				
				</td>
				</tr>
				<tr>
				<td width="13%">Postcode</td><td width="1%">:</td><td width="29%">
				<input type="text" name="postcode" value="<?= $b['postcode'] ?>" size="30" maxlength="200">
				</td>
				<td width="16%">City</td><td width="1%">:</td><td width="40%">
				<input type="text" name="city" value="<?= $b['city'] ?>" size="30" maxlength="200">
				
				</td>
				</tr>
				
				
			<tr>
				<td>State</td><td>:</td><td><div id="divResult">
								<?
									$get_place=mysql_query("select * from os_state where country_code=1 order by order_id asc");
								?>
								<select name="state" class="text">
									<option value="">Please select</option>
								<? 
									while($s=mysql_fetch_array($get_place,MYSQL_ASSOC))
									{
										if($s['id']==$b['state'])
										{
											$sslt="selected";
										}
										else
										{
											$sslt="";
										}	
								?>
									<option <?= $sslt ?> value="<?= $s['id'] ?>"><?= $s['name_eng'] ?></option>
								<? 
										
									}
								?>
								</select>
								</div>
					</td>
				<td>Country</td><td>:</td><td>
				<?
					$get_place=mysql_query("select * from os_country where id=1");
				?>
				<select name="country" class="text" onChange="showPlace(this.value,'divResult','state')">
					
				<? 
					while($s=mysql_fetch_array($get_place,MYSQL_ASSOC))
					{
						if($s['id']==$b['country'])
						{
							$desslt="selected";
						}
						else
						{
							$desslt="";
						}
						
				?>
					<option value="<?= $s['id'] ?>" <?= $desslt ?>><?= $s['engname'] ?></option>
				<? 
						
					}
				?>
				</select>
				</td>
			</tr>
			<tr valign="top">
				<td>Message</td><td>:</td><td colspan="4">
				<textarea rows="5" cols="35" name="message" class="text" ><?= $b['message'] ?></textarea>
				</td>
			</tr>
			<tr>
				<td colspan="3" align="left"> 
				<input type="hidden" value="<?= $b['id'] ?>" name="id">
				<input type="submit" value="Update" class="button" name="btnupdate_info"> </td>
			</tr>
			
				</table>
			</form>
			</fieldset>	
			<br>
				</td>
			</tr>
			
			
				<?
					if($b['cc_status']==1)
					{	
						$get_cc=mysql_query("select * from os_ccrecord where bill_id=$bid");
						
						if(mysql_num_rows($get_cc)>0)
						{
							$cc=mysql_fetch_array($get_cc,MYSQL_ASSOC);
						
				?>
				<tr>
					<Td colspan="5"><br>
						<table width="100%">
							<td colspan="6"><b>Credit Card Payment record</b></td>
							<tr>
								<td width="20%">Transaction Id</td><td width="1%">:</td><td width="79%"><?= $cc['tran_id'] ?></td>
							</tr>
							<tr>
								<td>Approval Code</td><td>:</td><td><?= $cc['app_code'] ?></td>
							</tr>
							<tr>
								<td>Amount Paid</td><td>:</td><td><?= $cc['currency'] ?> <?= $cc['bill_amount'] ?></td>
							</tr>
							<tr>
								<td>Transaction Date &amp; Time</td><td>:</td><Td><?= $cc['payment_date'] ?></Td>
							</tr>
						</table>
					
					</Td>
				
				</tr>
				
				
				<? }} ?>
			
			
			<?
				$get_payment=mysql_query("select * from os_payment_record where bid=$bid order by create_datetime asc");
				if(mysql_num_rows($get_payment)>0)
				{
			?>
				<tr>
				<td>
				<table width="100%" cellpadding="0" cellspacing="0" class="solidborder">
				<?
					$no=1;
					while($p=mysql_fetch_array($get_payment))
					{
				?>
				<tr>
					<td width="100%">
						<table width="100%">
							<td width="13%" colspan="6" bgcolor="#F3F3F3"><b>Payment record <?= $no ?></b></td>
							<tr>
								<td>Payment date</td><td width="1%">:</td><td width="29%"><?= $p['paymentdate'] ?></td>
								<td width="16%">Payment amount</td><td width="1%">:</td><td width="40%">RM <?= number_format($p['paidamount'],2) ?></td>
							</tr>
							<tr>
								<td>Payment bank</td><td>:</td><td><?= $p['paymentbank'] ?></td>
								<td>Account number</td><td>:</td><td><?= $p['accountnumber'] ?></td>
							</tr>
							<tr>
								<td >remark</td><td>:</td ><td colspan="4">
								
								<?= $p['message'] ?></td>
							</tr>
							<tr>
								
							</tr>
						</table>
					
					</td>
					
				</tr>
				<?
					$no++; } ?>
				</table>
				</td>
			</tr>
			
			<? } ?>		  
		</table>
		
			
		</Td>  
</table>

<? require("required/footer.php"); ?>	